package com.hae.shop.common.base;

import org.springframework.util.StringUtils;

public class Response<T> {
    private Boolean success;
    private T result;
    private String errorCode;
    private String errorMsg;

    public Response() {
    }

    public Response(T result) {
        this.success = Boolean.TRUE;
        this.result = result;
    }

    public Response(String errorCode, String errorMsg) {
        this.success = Boolean.FALSE;
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    /**
     * 必须存在，否则反序列化时无法对success赋值，false永远是空
     */
    public Boolean getSuccess() {
        return success;
    }

    /**
     * 必须存在，否则反序列化时无法对success赋值，false永远是空
     * @param success
     */
    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public boolean isSuccess() {
        return success;
    }

    public T getResult() {
        return result;
    }

    /**
     * 只有result不为null时才进行赋值
     * @param result
     */
    public void setResult(T result) {
        if (null != result) {
            this.success = Boolean.TRUE;
            this.result = result;
        }
    }

    public void setResult(String errorCode, String errorMsg) {
        this.success = Boolean.FALSE;
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public String getErrorCode() {
        return errorCode;
    }

    /**
     * 只有errorCode不为null时才进行赋值
     * @param errorCode
     */
    public void setErrorCode(String errorCode) {
        if (!StringUtils.isEmpty(errorCode)) {
            this.success = Boolean.FALSE;
            this.errorCode = errorCode;
        }
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    /**
     * 只有errorMsg不为null时才进行赋值
     * @param errorMsg
     */
    public void setErrorMsg(String errorMsg) {
        if (!StringUtils.isEmpty(errorMsg)) {
            this.success = Boolean.FALSE;
            this.errorMsg = errorMsg;
        }
    }

    @Override
    public String toString() {
        return "Response{" +
                "success=" + success +
                ", result=" + result +
                ", errorCode='" + errorCode + '\'' +
                ", errorMsg='" + errorMsg + '\'' +
                '}';
    }
}
